
package com.hlw.hr_man.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hlw.hr_man.service.EmployeeSrevice;
import com.hlw.hr_man.service.RoleService;
import com.hlw.hr_man.serviceImpl.EmployeeServiceImpl;
import com.hlw.hr_man.serviceImpl.RoleServiceImpl;
/**
 * 更新用户角色
 * @author 李豪
 *
 */
public class UpdateRolecontroller extends HttpServlet {
	EmployeeSrevice employeeservice = new EmployeeServiceImpl();
	RoleService ros = new RoleServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		PrintWriter pw = resp.getWriter();
		String dept_name = req.getParameter("dept_name");
		String e_number = req.getParameter("e_number");
		String role_id = req.getParameter("role_id");
		int i = Integer.parseInt(req.getParameter("role"));
		//如果有此部门已经有部门经理，则提示且不能修改角色
		if (i == 1) {
			if (!role_id.equals("2")) {
				pw.write("您未做任何修改！");
			} else {
				String rolename = dept_name + "经理";
				int roleid = ros.findRoleIdByRoleName(rolename);
				int flag = employeeservice.findEmployeeByRoleid(roleid);
				if (flag == 1) {
					pw.write("当前部门已有部门经理，若要修改此员工请先将此部门的部门经理撤职！");
				} else {
					int flag1 = employeeservice.updateRoleToManger(e_number, roleid);
					if (flag1 > 0) {
						pw.write("已将当前员工升职为部门经理！");

					}
					else{
						pw.write("修改失败！");
					}
				}
			}
		}
		//如果将部门经理角色改为普通员工，则提示管理员为该部门添加部门经理
		if (i == 2) {
			if (role_id.equals("2")) {
				pw.write("您未做任何修改！");
			} else {
				int flag = employeeservice.updateRoleToNormal(e_number);
				if (flag > 0) {
					pw.write("修改成普通员工成功！请及时为" + dept_name + "添加部门经理");
				}
				else{
					pw.write("修改失败！");
				}
			}
		}
		
	}
}
